package mirrormonkey.util.serializer;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.jme3.math.Quaternion;
import com.jme3.network.serializing.Serializer;

/**
 * Serializes <tt>Quaternions</tt> by writing and reading their <tt>w</tt>,
 * <tt>x</tt>, <tt>y</tt> and <tt>z</tt> values.
 * 
 * @author Philipp Christian Loewner
 * 
 */
public class QuaternionSerializer extends Serializer {

	@Override
	@SuppressWarnings("unchecked")
	public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
		Quaternion q = new Quaternion(data.getFloat(), data.getFloat(),
				data.getFloat(), data.getFloat());
		return (T) q;
	}

	@Override
	public void writeObject(ByteBuffer buffer, Object object)
			throws IOException {
		Quaternion q = (Quaternion) object;
		buffer.putFloat(q.getX());
		buffer.putFloat(q.getY());
		buffer.putFloat(q.getZ());
		buffer.putFloat(q.getW());
	}

}
